package com.telenav;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class Struct33Dialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private ArrayList<Text> text_elements = new ArrayList<Text>();

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Struct33Dialog(Shell parent, int style) {
		super(parent, style);
		setText("Struct 3x3 Parameter");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					sb.append(((text_elements.get(i*3+j).getText()==null || text_elements.get(i*3+j).getText().equals(""))?("0"):text_elements.get(i*3+j).getText())+",");
				}
			}
			sb.setLength(sb.length()-1);
			result = sb.toString();
			
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(300, 320);
		shell.setText(getText());
		shell.setLayout(null);

		Group group = new Group(shell, SWT.NONE);
		group.setBounds(10, 10, 240, 200);
		group.setText("3x3 Element");

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				Text text_element = new Text(group, SWT.BORDER);
				text_element.setBounds(j * 40 + 10, i * 40 + 20, 30, 20);
				text_element.setText("1");
				text_elements.add(text_element);
			}
		}

	}
}
